package com.dfrz.servlet;

import com.alibaba.fastjson.JSONObject;
import com.dfrz.dao.*;
import com.dfrz.entry.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "DoAnythingServlet",urlPatterns = "/doanything")
public class DoAnythingServlet extends BaseServlet {
    //登录
   public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
       Result result = new Result();
       String loginName=request.getParameter("name");
       String passWord=request.getParameter("passWord");
       String identity=request.getParameter("category");
       if(identity!=null||!"".equals(identity)){
           if ("用户".equals(identity)){
               UserDao userdao=new UserDao();
               User user=new User();
               user=userdao.login(loginName,passWord);
               if(user!=null){
                   result.setCode(0);
                   result.setMessage("登录成功");
                   result.setData(identity);
                   request.getSession().setAttribute("userInfo",user);

               }else{
                   result.setCode(-1);
                   result.setMessage("账号密码错误");
               }
               response.getWriter().write(JSONObject.toJSONString(result));

           }else if ("商家".equals(identity)){
               BusinessInfoDao businessInfodao=new BusinessInfoDao();
               BusinessInfo businessInfo=businessInfodao.buslogin(loginName,passWord);
               if(businessInfo!=null){
                   result.setCode(0);
                   result.setMessage("登录成功");
                   result.setData(identity);
                   request.getSession().setAttribute("businessInfo",businessInfo);
               }else{
                   result.setCode(-1);
                   result.setMessage("账号密码错误");
               }
               response.getWriter().write(JSONObject.toJSONString(result));


           }else if("骑手".equals(identity)){
               DeliveryInfoDao deliveryInfodao=new DeliveryInfoDao();
               DeliveryInfo deliveryinfo=deliveryInfodao.login(loginName,passWord);
               if(deliveryinfo!=null){
                   result.setCode(0);
                   result.setMessage("登录成功");
                   result.setData(identity);
                   request.getSession().setAttribute("deliveryinfo",deliveryinfo);
               }else{
                   result.setCode(-1);
                   result.setMessage("账号密码错误");
               }

               response.getWriter().write(JSONObject.toJSONString(result));
           }


       }else{
           result.setCode(-1);
           result.setMessage("请选择要登录的账号类型！");
       }


   }
   //注册
   public void register(HttpServletRequest request, HttpServletResponse response) throws IOException {
       Result result = new Result();
       String identity = request.getParameter("category");
//       String name = request.getParameter("name");
//       if (name == null || "".equals(name)) {
//           result.setCode(-1);
//           result.setMessage("姓名不能为空");
//           response.getWriter().write(JSONObject.toJSONString(result));
//           return;
//       }
       String loginName = request.getParameter("loginName");
       if (loginName == null || "".equals(loginName)) {
           result.setCode(-1);
           result.setMessage("用户名不能为空");
           response.getWriter().write(JSONObject.toJSONString(result));
           return;
       }
       String passWord = request.getParameter("password");
       if (passWord == null || "".equals(passWord)) {
           result.setCode(-1);
           result.setMessage("密码不能为空");
           response.getWriter().write(JSONObject.toJSONString(result));
           return;
       }
       String againPassWord = request.getParameter("againPassWord");
       if (againPassWord == null || "".equals(againPassWord)) {
           result.setCode(-1);
           result.setMessage("再次密码不能为空");
           response.getWriter().write(JSONObject.toJSONString(result));
           return;
       }
       String phone = request.getParameter("phone");
       if (phone == null || "".equals(phone)) {
           result.setCode(-1);
           result.setMessage("电话不能为空");
           response.getWriter().write(JSONObject.toJSONString(result));
           return;
       }
       if (!passWord.equals(againPassWord)) {
           result.setCode(-1);
           result.setMessage("两次密码不一致");
           response.getWriter().write(JSONObject.toJSONString(result));
           return;
       }
       String check=request.getParameter("checkbox");
       if(check==null||"".equals(check)){
           result.setCode(-1);
           result.setMessage("请勾选确定协议");
           response.getWriter().write(JSONObject.toJSONString(result));
           return;
       }
       if (identity != null || !"".equals(identity)) {
           if ("用户".equals(identity)) {
               User user = new User();
               UserDao userdao = new UserDao();
               user = userdao.queryUserByType(loginName, 1);
               if (user != null) {
                   result.setCode(-1);
                   result.setMessage("用户名重复");
                   response.getWriter().write(JSONObject.toJSONString(result));
                   return;
               }
               user = userdao.queryUserByType(phone, 2);
               if (user != null) {
                   result.setCode(-1);
                   result.setMessage("电话重复");
                   response.getWriter().write(JSONObject.toJSONString(result));
                   return;
               }
               User user1 = new User();
//               user1.setName(name);
               user1.setLoginName(loginName);
               user1.setPassWord(passWord);
               user1.setPhone(phone);
//               user1.setGender("男");
//               user1.setAge(11);
//               user1.seteMail("12121@qq.com");
               int count = userdao.insertUser(user1);
               if (count > 0) {
                   result.setCode(0);
                   result.setMessage("注册成功");
                   response.getWriter().write(JSONObject.toJSONString(result));

               }

           }
           if ("商家".equals(identity)) {
               BusinessInfo businessInfo = new BusinessInfo();
               BusinessInfoDao businessInfodao = new BusinessInfoDao();
               businessInfo = businessInfodao.queryBusinessByType(loginName, 1);
               if (businessInfo != null) {
                   result.setCode(-1);
                   result.setMessage("用户名重复");
                   response.getWriter().write(JSONObject.toJSONString(result));
                   return;
               }
               businessInfo = businessInfodao.queryBusinessByType(phone, 2);
               if (businessInfo != null) {
                   result.setCode(-1);
                   result.setMessage("电话重复");
                   response.getWriter().write(JSONObject.toJSONString(result));
                   return;
               }
               BusinessInfo businessInfo1 = new BusinessInfo();
//               businessInfo1.setName(name);
               businessInfo1.setLoginname(loginName);
               businessInfo1.setPassword(passWord);
               businessInfo1.setPhone(phone);
//               user1.setGender("男");
//               user1.setAge(11);
//               user1.seteMail("12121@qq.com");
               int count = businessInfodao.insertBusiness(businessInfo1);
               if (count > 0) {
                   result.setCode(0);
                   result.setMessage("注册成功");
                   response.getWriter().write(JSONObject.toJSONString(result));

               }

           }
           if ("骑手".equals(identity)) {
               DeliveryInfo deliveryinfo=new DeliveryInfo();
               DeliveryInfoDao deliveryInfodao=new DeliveryInfoDao();
               deliveryinfo = deliveryInfodao.queryDeliveryByType(loginName, 1);
               if (deliveryinfo != null) {
                   result.setCode(-1);
                   result.setMessage("用户名重复");
                   response.getWriter().write(JSONObject.toJSONString(result));
                   return;
               }
               deliveryinfo = deliveryInfodao.queryDeliveryByType(phone, 2);
               if (deliveryinfo != null) {
                   result.setCode(-1);
                   result.setMessage("电话重复");
                   response.getWriter().write(JSONObject.toJSONString(result));
                   return;
               }
               DeliveryInfo deliveryinfo1 = new DeliveryInfo();
//               deliveryinfo1.setName(name);
               deliveryinfo1.setLoginName(loginName);
               deliveryinfo1.setPassWord(passWord);
               deliveryinfo1.setPhone(phone);
//               user1.setGender("男");
//               user1.setAge(11);
//               user1.seteMail("12121@qq.com");
               int count = deliveryInfodao.insertDelivery(deliveryinfo1);
               if (count > 0) {
                   result.setCode(0);
                   result.setMessage("注册成功");
                   response.getWriter().write(JSONObject.toJSONString(result));

               }

           }


       }
   }
   //轮播图
   public void carouselMap(HttpServletRequest request, HttpServletResponse response) throws IOException {
       List<Carousel> list=null;
       Result result=new Result();
       CarouselDao carouselDao=new CarouselDao();
       list=carouselDao.queryCarousel();
       if (list!= null){
           result.setCode(0);
           result.setMessage("查询成功");
           result.setData(list);
           response.getWriter().write(JSONObject.toJSONString(result));
       }
       else{
           result.setCode(-1);
           result.setMessage("查询异常");
           response.getWriter().write(JSONObject.toJSONString(result));
       }
   }
   //头像和用户名显示
   public void userinfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
       Result result = new Result();
      User user= (User) request.getSession().getAttribute("userInfo");
      if(user!=null){
          result.setCode(0);
          result.setData(user);
          response.getWriter().write(JSONObject.toJSONString(result));

      }else{
          result.setCode(-1);
          result.setMessage("请登录");
          response.getWriter().write(JSONObject.toJSONString(result));
      }

   }
   //回显用户信息
   public void feedBackInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
       User user= (User) request.getSession().getAttribute("userInfo");
       Result result=new Result();
       if(user!=null){
          result.setCode(0);
          result.setData(user);
           response.getWriter().write(JSONObject.toJSONString(result));
       }else{
           result.setCode(-1);
           result.setMessage("用户未登录");
           response.getWriter().write(JSONObject.toJSONString(result));
       }

   }
   //完善或更新用户信息
   public void advanceUserInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
       Result result = new Result();
       User user= (User) request.getSession().getAttribute("userInfo");
        String name=request.getParameter("name");
        String loginName=request.getParameter("loginName");
        String passWord=request.getParameter("passWord");
        String gender=request.getParameter("gender");
        Integer age=Integer.valueOf(request.getParameter("age"));
        String phone=request.getParameter("phone");
//        String eMail=request.getParameter("eMail");
        User user1=new User();
        UserDao userdao = new UserDao();
        user1=userdao.queryUserByType(loginName,1);
        if(user1!=null){
            if(user1.getId()!=user.getId()){
                result.setCode(-1);
                result.setMessage("用户名已经存在");
                response.setContentType("application/json; charset=UTF-8");
                response.getWriter().write(JSONObject.toJSONString(result));
                return;
            }
        }
        user1=userdao.queryUserByType(phone,2);
        if (user1!=null){
            if(user1.getId()!=user.getId()){
                result.setCode(-1);
                result.setMessage("电话号码已经存在");
                response.setContentType("application/json; charset=UTF-8");
                response.getWriter().write(JSONObject.toJSONString(result));
                return;
            }
        }
        User user2=new User();
        user2.setName(name);
        user2.setLoginName(loginName);
        user2.setPassWord(passWord);
        user2.setAge(age);
        user2.setGender(gender);
        user2.setPhone(phone);
        int count=userdao.updateUser(user2);
        if (count>0){
            result.setCode(0);
            result.setMessage("更新成功");
            response.getWriter().write(JSONObject.toJSONString(result));
        }else{
            result.setCode(-1);
            result.setMessage("更新失败");
            response.getWriter().write(JSONObject.toJSONString(result));
        }




   }
   //初始化菜单
   public void initMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
       List<GoodsInfo>list=null;
       GoodsInfoDao goodsInfoDao=new GoodsInfoDao();
       Result result = new Result();
       list=goodsInfoDao.getAllGoods();
       if(list!=null){
           result.setCode(0);
           result.setMessage("查询成功");
           result.setData(list);
           response.getWriter().write(JSONObject.toJSONString(result));
       }else{
           result.setCode(-1);
           result.setMessage("查询失败");
           response.getWriter().write(JSONObject.toJSONString(result));
       }




   }
   //添加购物车
   public void addToCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
       Result result = new Result();
       GoodsInfo goodsInfo = new GoodsInfo();
       GoodsInfoDao goodsInfoDao = new GoodsInfoDao();
       ShoppingCar shoppingCar=new ShoppingCar();
       ShoppingCarDao shoppingCarDao = new ShoppingCarDao();
       User userinfo = (User) request.getSession().getAttribute("userInfo");
       if (userinfo == null) {
           result.setCode(-1);
           result.setMessage("请登录");
           response.getWriter().write(JSONObject.toJSONString(result));
           return;
       }

       int number = Integer.valueOf(request.getParameter("num"));
       if (number==0){
           result.setCode(-1);
           result.setMessage("请选择购买的数量");
           response.getWriter().write(JSONObject.toJSONString(result));
           return;
       }
       int goodsid = Integer.valueOf(request.getParameter("goodsid"));
       goodsInfo = goodsInfoDao.queryGoodsById(goodsid);
       int price=goodsInfo.getCurprice();
       shoppingCar=shoppingCarDao.queyrcCarById(goodsid,userinfo.getId());
       if(shoppingCar!=null){
           ShoppingCar shoppingCar1=new ShoppingCar();
           shoppingCar1.setGoodsid(shoppingCar.getGoodsid());
           shoppingCar1.setUserid(shoppingCar.getUserid());
           shoppingCar1.setNumber((shoppingCar.getNumber()+number));
           shoppingCar1.setTotalprice((shoppingCar.getTotalprice()+number*price));
           int count=shoppingCarDao.update_goodsnum(shoppingCar1);
           if (count>0){
               result.setCode(0);
               result.setMessage("更新数量成功");
               response.getWriter().write(JSONObject.toJSONString(result));
               return;
           }else{
               result.setCode(-1);
               result.setMessage("更新数量失败");
               response.getWriter().write(JSONObject.toJSONString(result));
               return;
           }

       }
//       ShoppingCar car=new ShoppingCar();
//       car=shoppingCarDao.queyrcCarById(goodsid,userinfo.getId());
//       if(car!=null){
//
//
//       }

       if (goodsInfo != null) {
           ShoppingCar car=new ShoppingCar();
           String goodsname=goodsInfo.getName();
           String imgURL=goodsInfo.getImgURL();
           car.setSinglePrice(price);
           car.setGoodsid(goodsid);
           car.setImgURL(imgURL);
           car.setGoodsname(goodsname);
           car.setNumber(number);
           car.setUserid(userinfo.getId());
           car.setTotalprice((number*price));
           int count=shoppingCarDao.insertShoppingCar(car);
           if (count>0){
               result.setCode(0);
               result.setMessage("添加购物车成功");
               response.getWriter().write(JSONObject.toJSONString(result));
           }else{
               result.setCode(-1);
               result.setMessage("添加购物车失败");
               response.getWriter().write(JSONObject.toJSONString(result));
           }


       }
   }
   //显示当前购物车里商品的数量
   public void showCartNum(HttpServletRequest request, HttpServletResponse response) throws IOException {
       User userinfo= (User) request.getSession().getAttribute("userInfo");
       ShoppingCarDao shoppingCarDao= new ShoppingCarDao();
       List<ShoppingCar>list=new ArrayList<ShoppingCar>();
       Result result = new Result();
       if(userinfo!=null){
           list=shoppingCarDao.querySomeoneCars(userinfo.getId());
           if(list.size()>0){
               int count=0;
               for (ShoppingCar shoppingCar : list) {
                   count+=shoppingCar.getNumber();
               }
               result.setCode(0);
               result.setData(count);
               response.getWriter().write(JSONObject.toJSONString(result));
           }else{
               result.setCode(-1);
               result.setMessage("购物车为空");
               result.setData(0);
               response.getWriter().write(JSONObject.toJSONString(result));
           }
       }



   }
   //显示购物车
   public void showSomeoneCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
       User userinfo= (User) request.getSession().getAttribute("userInfo");
       ShoppingCarDao shoppingCarDao= new ShoppingCarDao();
       List<ShoppingCar>list=new ArrayList<ShoppingCar>();
       Result result = new Result();
       if(userinfo!=null){
           list=shoppingCarDao.querySomeoneCars(userinfo.getId());
           if (list!=null){
               result.setCode(0);
               result.setData(list);
               response.getWriter().write(JSONObject.toJSONString(result));
           }else{
               result.setCode(-1);
               response.getWriter().write(JSONObject.toJSONString(result));

           }
       }

   }
   //增加购物车的商品数量
    public void plusCartNum(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User userinfo= (User) request.getSession().getAttribute("userInfo");
        ShoppingCarDao shoppingCarDao= new ShoppingCarDao();
        Result result = new Result();
        ShoppingCar shoppingCar=new ShoppingCar();
        Integer goodsid=Integer.valueOf(request.getParameter("goodsid"));

        if(userinfo!=null){
            shoppingCar=shoppingCarDao.queyrcCarById(goodsid,userinfo.getId());
            if(shoppingCar!=null){
                ShoppingCar shoppingCar1=new ShoppingCar();
                shoppingCar1.setUserid(userinfo.getId());
                shoppingCar1.setGoodsid(goodsid);
                shoppingCar1.setNumber((shoppingCar.getNumber()+1));
                shoppingCar1.setTotalprice(shoppingCar.getSinglePrice()+shoppingCar.getTotalprice());
                int count=shoppingCarDao.update_goodsnum(shoppingCar1);
                if(count>0){
                    result.setCode(0);
                    result.setMessage("添加数量成功");
                    response.getWriter().write(JSONObject.toJSONString(result));
                }else{
                    result.setCode(-1);
                    result.setMessage("添加数量失败");
                }
            }
        }


    }
    //减少购物车的商品数量
    public void minusCartNum(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User userinfo= (User) request.getSession().getAttribute("userInfo");
        ShoppingCarDao shoppingCarDao= new ShoppingCarDao();
        Result result = new Result();
        ShoppingCar shoppingCar=new ShoppingCar();
        Integer goodsid=Integer.valueOf(request.getParameter("goodsid"));

        if(userinfo!=null){
            shoppingCar=shoppingCarDao.queyrcCarById(goodsid,userinfo.getId());
            if(shoppingCar!=null){
                ShoppingCar shoppingCar1=new ShoppingCar();
                shoppingCar1.setUserid(userinfo.getId());
                shoppingCar1.setGoodsid(goodsid);
                shoppingCar1.setNumber((shoppingCar.getNumber()-1));
                shoppingCar1.setTotalprice(shoppingCar.getTotalprice()-shoppingCar.getSinglePrice());
                int count=shoppingCarDao.update_goodsnum(shoppingCar1);
                if(count>0){
                    result.setCode(0);
                    result.setMessage("减少数量成功");
                    response.getWriter().write(JSONObject.toJSONString(result));
                }else{
                    result.setCode(-1);
                    result.setMessage("减少数量失败");
                }
            }
        }


    }
    //显示购物车里商品总价钱
    public void showCartTotalPrice(HttpServletRequest request, HttpServletResponse response) throws IOException {
       User userinfo= (User) request.getSession().getAttribute("userInfo");
       Result result = new Result();
       List<ShoppingCar>list=new ArrayList<ShoppingCar>();
       ShoppingCarDao shoppingCarDao=new ShoppingCarDao();
       if(userinfo!=null){
           list=shoppingCarDao.querySomeoneCars(userinfo.getId());
           if (list.size()>0){
               int totalPrice=0;
               for (ShoppingCar shoppingCar : list) {
                   totalPrice+=shoppingCar.getTotalprice();
               }
               result.setCode(0);
               result.setData(totalPrice);
               response.getWriter().write(JSONObject.toJSONString(result));
           }else {
               result.setCode(-1);
               response.getWriter().write(JSONObject.toJSONString(result));
           }

       }

    }
    //删除购物车商品
    public void deleteShoppingCar(HttpServletRequest request, HttpServletResponse response) throws IOException {
       User userinfo= (User) request.getSession().getAttribute("userInfo");
       Result result = new Result();
        Integer goodsid=Integer.valueOf(request.getParameter("goodsid"));
        ShoppingCarDao shoppingCarDao= new ShoppingCarDao();
        if(userinfo!=null){
            ShoppingCar shoppingCar=new ShoppingCar();
            shoppingCar=shoppingCarDao.queyrcCarById(goodsid,userinfo.getId());
            if(shoppingCar!=null){
                ShoppingCar shoppingCar1=new ShoppingCar();
                shoppingCar1.setUserid(userinfo.getId());
                shoppingCar1.setGoodsid(goodsid);
                int count=shoppingCarDao.deleteShoppingCar(shoppingCar1);
                if (count>0){
                    result.setCode(0);
                    result.setMessage("删除购物车成功");
                    response.getWriter().write(JSONObject.toJSONString(result));
                }
                else{
                    result.setCode(-1);
                    result.setMessage("删除购物车失败");
                    response.getWriter().write(JSONObject.toJSONString(result));
                }
            }
        }

    }

}
